package com.testing.DriverSelf;

import com.testing.common.AutoUtils;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.time.Duration;

/**
 * @Classname AppDriver
 * @Description 类型说明
 * @Date 2022/10/26 20:07
 * @Created by 特斯汀Roy
 */
public class MobileBrowserDriver {

    //全局的driver对象。
    private AndroidDriver driver;

    /**
     * 如果启动的时候，想直接从桌面启动，package和activity的值给空字符串就行了。
     *
     * @param port
     * @param deviceName
     */
    public MobileBrowserDriver(String port, String deviceName) {
        try {
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setCapability("platformName", "Android");
            cap.setCapability("noReset", true);
            cap.setCapability("browserName", "Browser");
            cap.setCapability("skipServerInstallation", true);
            cap.setCapability("udid", deviceName);
            cap.setCapability("chromeDriverExecutable", "E:\\AutoTools\\chromedriver\\92\\chromedriver.exe");

            //操作中文键盘输入
            cap.setCapability("unicodeKeyboard", true);
            cap.setCapability("resetKeyboard", true);

            driver = new AndroidDriver(new URL("http://127.0.0.1:" + port + "/wd/hub"), cap);
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        } catch (Exception e) {
            AutoUtils.log.error("启动APP失败，请查看appium服务端的日志", e.fillInStackTrace());
        }
    }

    public AndroidDriver getDriver() {
        return driver;
    }

    public void setDriver(AndroidDriver driver) {
        this.driver = driver;
    }
}
